package com.coderlin.controller;

import com.coderlin.entities.Customer;
import com.coderlin.entities.History;
import com.coderlin.entities.Worker;
import com.coderlin.service.CustomerService;
import com.coderlin.service.HistoryService;
import com.coderlin.service.WorkerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.List;

/**
 * <Description> <br>
 *
 * @author Coder_Lin<br>
 * @version 1.0<br>
 * @taskId: <br>
 * @createDate 2021/07/24 9:45 <br>
 * @see com.coderlin.controller <br>
 */
@Controller
@RequestMapping("/history")
@Slf4j
public class HistoryController {

    @Autowired
    private HistoryService historyService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private WorkerService workerService;

    @RequestMapping("/listHistory")
    public String listHistory(Model model){
        List<History> histories = historyService.listHistory();

        model.addAttribute("historyList",histories);
        return "historyList";
    }

    @RequestMapping("/addHistoryPre")
    public String addHistoryPre(Model model){
        List<Customer> customers = customerService.listCustomer();
        List<Worker> workers = workerService.listWorker();
        model.addAttribute("workerList",workers);
        model.addAttribute("customerList",customers);

        return "historyAdd";
    }

    @RequestMapping("/addHistory")
    public String addHistory(History history,Integer cid, Integer wid){
        Integer integer = historyService.addHistory(history,cid,wid);


        return "redirect:/history/listHistory";
    }

    @RequestMapping("/delHistory/{hid}")
    public String delHistory(@PathVariable("hid") int hid){
        Integer integer = historyService.delHistoryById(hid);

        return "redirect:/history/listHistory";
    }

    @RequestMapping("/modHistoryPre/{hid}")
    public String modHistoryPre(@PathVariable("hid") int hid,Model model){
        //依照ID查出History
        History history = historyService.listHistoryById(hid);
        List<Customer> customers = customerService.listCustomer();
        List<Worker> workers = workerService.listWorker();
        model.addAttribute("workerList",workers);
        model.addAttribute("customerList",customers);
        model.addAttribute("history",history);


        return "historyMod";
    }

    @RequestMapping("/modHistory")
    public String modHistory(History history,Integer cid, Integer wid){
        Integer integer = historyService.modHistory(history, cid, wid);

        return "redirect:/history/listHistory";
    }

    @RequestMapping("/searchHistory")
    public String searchHistory(String sendWaterTimeStart, String sendWaterTimeEnd, Model model){
        if (log.isInfoEnabled()) {
            log.info("sendWaterTimeStart:"+sendWaterTimeStart);
            log.info("sendWaterTimeEnd:"+sendWaterTimeEnd);
        }

        List<History> histories = null;
        try {
            histories = historyService.searchHistoryByTimeRange(sendWaterTimeStart, sendWaterTimeEnd);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        model.addAttribute("historyList",histories);

        return "historyList";
    }

    @RequestMapping(value = "/batchDeleteHistory",method = RequestMethod.POST)
    @ResponseBody
    public String batchDeleteHistory(String idList,Model model){

        Integer integer = null;
        try {
            integer = historyService.batchDeleteHistoryById(idList);
            if(integer<=0){
                return "fail";
            }else{
                return "success";
            }
        } catch (Exception e) {
            return "error";
        }

    }
}
